<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="核销码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入核销码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="核销码" align="center" prop="code" />
      <el-table-column label="用户信息" align="center" prop="userInfo" min-width="100px">
        <template #default="scope">
          <el-avatar
            fit="cover"
            :src="scope.row.user?.avatar || '/avatar.png'"
            class="flex-none w-50px h-50px"
          />
          <br />
          <el-text>{{ scope.row.user?.nickname || '匿名' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        label="所属店铺"
        align="center"
        prop="shopInfo"
        v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
        min-width="120px"
      >
        <template #default="scope">
          <span class="ml-5px">{{ scope.row.shop?.name || '未知' }}</span>
          <br />
          <img
            :src="scope.row.shop?.logo || '/placeholder.png'"
            class="w-40px border border-solid border-gray-50"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单类型" align="center" prop="bizType" /> -->
      <el-table-column label="交易单号" align="center" prop="bizId" min-width="180px" />
      <el-table-column label="店员" align="center" prop="staffInfo" min-width="150px">
        <template #default="scope">
          <el-text>{{ scope.row.verifierName || '匿名' }}</el-text>
          <br />
          <el-text>{{ scope.row.verifierMobile || '未知手机号' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" width="120px" fixed="right">
        <template #default="scope">
          <el-button
            v-hasPermi="['trade:order:query']"
            link
            type="primary"
            @click="openOrderDetail(scope.row.bizId)"
          >
            <Icon icon="ep:notification" />
            查看订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { TradeVerifyRecordApi, TradeVerifyRecordVO } from '@/api/mall/trade/verify/record'

/** 核销记录 列表 */
defineOptions({ name: 'TradeVerifyRecord' })

const { push } = useRouter() // 路由跳转

const loading = ref(true) // 列表的加载中
const list = ref<TradeVerifyRecordVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TradeVerifyRecordApi.getVerifyRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 查看订单详情 */
const openOrderDetail = (id: number | string) => {
  push({ name: 'TradeOrderDetail', params: { id } })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
